function list()
{
	this.e = null;
	this.add_tail = list_add_tail;
	this.add_head = list_add_head;
	this.remove = list_remove;
	this.empty = function() { return this.e == null; }
	this.is_singleton = function() { if(this.e == null) return false; if(this.e.next == this.e) return true; return false; }
	this.foreach = list_foreach;
	this.next = function(e) { return e.next; }
	this.prev = function(e) { return e.prev; }
	this.head = function() { if(this.e == null) return null; return this.e; }
	this.tail = function() { if(this.e == null) return null; return this.e.prev; }
}

function list_add_tail(e)
{
	if(this.e == null)
	{
		e.next = e;
		e.prev = e;
		this.e = e;
	}
	else
	{
		this.e.prev.next = e;
		e.prev = this.e.prev;
		e.next = this.e;
		this.e.prev = e;
	}
}

function list_add_head(e)
{
	if(this.e == null)
	{
		e.next = e;
		e.prev = e;
		this.e = e;
	}
	else
	{
		e.next = this.e;
		e.prev = this.e.prev;
		this.e.prev.next = e;
		this.e.prev = e;
		this.e = e;
	}
}

function list_remove(e)
{
	var next = (e.next == e?null:e.next);
	e.prev.next = e.next;
	e.next.prev = e.prev;
	e.next = null;
	e.prev = null;
	if(this.e == e)
	{
		this.e = next;
	}
}

function list_foreach(f, param)
{
	if(this.e == null) return;
	var it = this.e;
	do
	{
		f(it, param);
		it = it.next;
	} while(it != this.e)
}